package com.dh.project.controller;

import com.dh.project.model.Admin;
import com.dh.project.model.PageInfo;
import com.dh.project.model.PageModel;
import com.dh.project.other.ModelRes;
import com.dh.project.service.AdminService;
import com.dh.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/4/6.
 */
@Controller
public class SystemController {

    private AdminService adminService;

    @Autowired
    public SystemController(AdminService adminService){
        this.adminService = adminService;
    }

    /**
     * 后台登录
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "loginSystem" ,produces = "text/html;charset=utf-8")
    public Object loginSystem(String username,String password){
        Map map = new HashMap();
        map.put("admin_login_name",username);
        map.put("admin_login_password",password);
        map.put("isDel",1);
        List<Admin> admins = adminService.selectByMap(map);
        if (admins.size() == 0){
            return new ModelRes(ModelRes.Status.ERROR,"用户名或密码错误!");
        }
        return new ModelRes(admins);
    }

    /**
     * 添加管理员
     * @param admin_name
     * @param admin_phone
     * @param user_login_name
     * @param password
     * @param admin_type
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addAdmin" ,produces = "text/html;charset=utf-8")
    public Object addAdmin(String admin_name,String admin_phone, String user_login_name,String password,int admin_type){
        Map map = new HashMap();
        map.put("admin_phone",admin_phone);
        List<Admin> admins = adminService.selectByMap(map);
        if(admins.size() > 0){
            return new ModelRes(ModelRes.Status.ERROR,"该手机号码已存在!");
        }

        Admin admin = new Admin();
        admin.setAdmin_login_name(user_login_name);
        admin.setAdmin_login_password(password);
        admin.setAdmin_name(admin_name);
        admin.setAdmin_phone(admin_phone);
        admin.setAdmin_type(admin_type);
        admin.setCreatetime(String.valueOf(new Date().getTime()));
        admin.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = adminService.insert(admin);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败!");
        }
    }

    /**
     * 修改管理员信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateAdmin" ,produces = "text/html;charset=utf-8")
    public Object updateAdmin(Admin admin){
        if (admin.getAdmin_phone() != null){
            Map map = new HashMap();
            map.put("admin_phone",admin.getAdmin_phone());
            map.put("isDel",1);
            List<Admin> admins = adminService.selectByMap(map);
            if (admins.size() != 0){
                if (admins.get(0).getAdmin_id() != admin.getAdmin_id()){
                    return new ModelRes(ModelRes.Status.ERROR,"此电话号码重复，修改失败!");
                }
            }
        }
        Admin admin1 = adminService.selectById(admin.getAdmin_id());
        if (admin.getAdmin_name() != null){
            admin1.setAdmin_name(admin.getAdmin_name());
        }
        if (admin.getAdmin_phone() != null){
            admin1.setAdmin_phone(admin.getAdmin_phone());
        }
        if (admin.getAdmin_login_name() != null){
            admin1.setAdmin_login_name(admin.getAdmin_login_name());
        }
        if (admin.getAdmin_login_password() != null){
            admin1.setAdmin_login_password(admin.getAdmin_login_password());
        }
        boolean isSuccess = adminService.updateById(admin1);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败!");
        }
    }

    /**
     * 删除管理员
     * @param admin_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteAdmin" ,produces = "text/html;charset=utf-8")
    public Object deleteAdmin(int admin_id){
        boolean isSuccess = adminService.deleteById(admin_id);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"删除失败!");
        }
    }

    /**
     * 管理员列表
     * @param model
     * @return
     */
    @RequestMapping(value = "selectAdminList" ,produces = "text/html;charset=utf-8")
    public Object selectAdminList(Admin admin, Integer page,Model model){

        Map map = new HashMap();
        map.put("admin_name",admin.getAdmin_name());
        map.put("admin_id",admin.getAdmin_id());

        map.put("page_start",(page - 1)*10 );
        map.put("page_end", 10 );

        List<Admin> admins = adminService.selectAdminListByMap(map);

        PageModel pageModel = adminService.selectAdminListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHref(admin,1));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));
        model.addAttribute("admin",admin);
        model.addAttribute("admins",admins);
        model.addAttribute("pageInfo",pageInfo);
        return "admin_info";
    }

    @RequestMapping(value = "login" ,produces = "text/html;charset=utf-8")
    public String login(){
        return "login";
    }
}
